
package tetris.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 * A menu bar which all the menu options.
 * 
 * @author Agari NLN (agarin@uw.edu)
 * @version 3.0
 */
public class TheMenus extends JMenuBar
{

  /**
   * This is here to avoid warnings.
   */
  private static final long serialVersionUID = 1L;

  /**
   * The frame where the menu bar is to be added onto.
   */
  private final GUI my_frame;

  /**
   * Construct main menu bar.
   * 
   * @param the_frame the main frame.
   */
  public TheMenus(final GUI the_frame)
  {
    super();
    my_frame = the_frame;
    makeMenus();
  }

  /**
   * Add all the individual menus together to form the menu bar.
   */
  private void makeMenus()
  {
    add(makeFileMenu());
    add(makeHelpMenu());
  }

  /**
   * Create the help menu.
   * 
   * @return the created help menu.
   */
  private JMenuItem makeHelpMenu()
  {
    // Make the Help Menu
    JMenu help_menu;
    help_menu = new JMenu("Help");
    help_menu.setMnemonic(KeyEvent.VK_H);

    // Make menu option - About
    final JMenuItem about = new JMenuItem("About", KeyEvent.VK_A);
    help_menu.add(about);
    about.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(final ActionEvent the_event)
      {
        JOptionPane.showMessageDialog(null, "This assignmnet is so hard, its kicking my ass",
                                      "About Tetris", JOptionPane.PLAIN_MESSAGE);
      }
    });
    return help_menu;
  }

  /**
   * Create the file menu.
   * 
   * @return the created file menu.
   */
  private JMenuItem makeFileMenu()
  {
    // Make the file menu
    JMenu files_menu;
    files_menu = new JMenu("File");
    files_menu.setMnemonic(KeyEvent.VK_F);

    // Make menu option - Start new game
    final JMenuItem new_game = new JMenuItem("New Game", KeyEvent.VK_N);
    files_menu.add(new_game);
    new_game.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(final ActionEvent the_event)
      {
        my_frame.exit();
        final GUI new_frame = new GUI();
        new_frame.startRealGame();
      }
    });

    // Make menu option - pause the current game
    final JMenuItem pause_game = new JMenuItem("Pause Game", KeyEvent.VK_P);
    files_menu.add(pause_game);
    pause_game.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(final ActionEvent the_event)
      {
        my_frame.getBoard().stopTimer();
      }
    });

    // Make menu option - Un-pause the current game
    final JMenuItem un_pause = new JMenuItem("Un-Pause Game", KeyEvent.VK_P);
    files_menu.add(un_pause);
    un_pause.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(final ActionEvent the_event)
      {
        my_frame.getBoard().startTimer();
      }
    });

    // Make menu option separator
    files_menu.addSeparator();

    // Make menu option - quit
    final JMenuItem quit = new JMenuItem("Quit", KeyEvent.VK_Q);
    files_menu.add(quit);
    quit.addActionListener(new ActionListener()
    {
      public void actionPerformed(final ActionEvent the_event)
      {
        System.exit(0);
      }
    });
    return files_menu;
  }

}
